<?php
    namespace Home\Controller;

use Think\Controller;

    class KeshikaidanchaxunController extends Controller
    {
        public function _initialize()
        {
            header("Access-Control-Allow-Origin: *");

            header("Access-Control-Allow-Credentials: true");
      
            header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        }

        public function get_list()
        {
            $page = I("get.page");
            $limit = I("get.limit");
            $attr = I("get.attr");
            $begin_time = I("get.begin_time");
            $end_time = I("get.end_time");
            $type = I("get.type");
            $type_1 = I("get.type_1");
        
            $admin_uuid = I('admin_uuid');
            $admin_dept_list_str = get_user_dept_list($admin_uuid);

            if (empty($type)) {
                $type = I("post.type");
            }

            if (empty($begin_time)) {
                $begin_time = I("post.begin_time");
            }

            if (empty($end_time)) {
                $end_time = I("post.end_time");
            }

            if (empty($end_time)) {
                $end_time = I("post.end_time");
            }

            if (empty($begin_time)) {
                $begin_time = 0;
            } else {
                $begin_time = strtotime($begin_time);
            }

            if (empty($end_time)) {
                $end_time = 9999999999;
            } else {
                $end_time = strtotime($begin_time);
            }

            if (empty($type)) {
                $type = 1;
            }

            if (empty($type_1)) {
                $type_1 = 1;
            }

            if ($type == 1) {
                $keshi = "ORDERED_BY_DEPT";
            } else {
                $keshi = "PERFORMED_BY_DEPT";
            }

            if ($type_1 == 1) {
                $tujing = "OUTP_CLASS2_INCOME";
            } else {
                $tujing = "INP_CLASS2_INCOME";
            }

        
            $sql = "SELECT DECODE (GROUPING (a.ACCOUNT_DEPT_CODE), 1, 'HJH', a.ACCOUNT_DEPT_CODE)
            AS dept_code,
         DECODE (GROUPING (a.ACCOUNT_DEPT_CODE),
                 1, '合计',
                 MIN (NVL (a.account_dept_name, a.ACCOUNT_DEPT_CODE)))
            AS \"科室名称\",
         SUM (charges) \"合计\",
         
    ";
        
            $sql_list = "";

            $list = M("cbhs.cbhs_distribution_calc_schm")->select();
            for ($i=0; $i < count($list); $i++) {
                $sql_list .= "SUM (DECODE (b.reck_class, '".$list[$i]["item_class"]."', charges, 0)) \"".$list[$i]["item_class_name"]."\",";
            }

            $sql_list = rtrim($sql_list, ",");
        
            $sql .= $sql_list;

            $sql .= "FROM HISFACT.".$tujing." b, COMM.sys_dept_dict a
   WHERE     st_date >= $begin_time
         AND st_date < $end_time
         AND b.".$keshi." = a.dept_code(+)
		 AND a.dept_code in ($admin_dept_list_str)
GROUP BY ROLLUP (a.ACCOUNT_DEPT_CODE)
ORDER BY a.ACCOUNT_DEPT_CODE NULLS FIRST ";
        

            $data = M()->query($sql);




            for ($i=0; $i < count($data); $i++) {
                unset($data[$i]['dept_code']);
            }


        
            $json["data"] = $data;
            $json["code"] = 0;
            $json["count"] = 0;
            $json["msg"] = "";
        
            $this->ajaxReturn($json, "json");
        }

        public function get_one()
        {
            $uuid = I("post.uuid");

            $info = M("hisfact.inp_class2_income")->where("uuid = '$uuid'")->find();
            $info["dept_date"] = date("Y-m-d H:i:s", $info["dept_date"]);
            $json["data"]   = $info;
            $json["code"]   = 0;
            $json["msg"]    = "";

            $this->ajaxReturn($json, "json");
        }

        public function save_one()
        {
            $data = I("post.");
            $uuid = $data["uuid"];
            unset($data["uuid"]);

            $result = M("hisfact.inp_class2_income")->where("uuid = '$uuid'")->save($data);
            if ($result !== false) {
                $json["code"] = 0;
                $json["msg"] = "修改成功";
            } else {
                $json["code"] = -1;
                $json["msg"] = "修改失败";
            }
 
            $this->ajaxReturn($json, "json");
        }

        public function add_one()
        {
            $data = I("post.");
            $uuid = uuid();
        
            $data["dept_date"] = time();
            $result = M("hisfact.inp_class2_income")->add($data);
            if ($result !== false) {
                $json["code"] = 0;
                $json["msg"] = "添加成功";
            } else {
                $json["code"] = -1;
                $json["msg"] = "添加失败";
            }
 
            $this->ajaxReturn($json, "json");
        }

        public function get_col_list()
        {
            $col = [];
            $col[] = "合计";
            $col[] = "科室名称";
            $list = M("cbhs.cbhs_distribution_calc_schm")->select();
            for ($i=0; $i < count($list); $i++) {
                $col[] = $list[$i]["item_class_name"];
            }
            $json["data"] = $col;
            $json["code"] = 0;
            $json["count"] = 0;
            $json["msg"] = "";
        
            $this->ajaxReturn($json, "json");
        }
    }
